﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using OA.Bll.Personnel;
using OA.Log;
using OA.Manager;
using OA.Model;
using OA.Utility;

namespace OA.Bll.Authority
{
    public class UserInfoPresenter : BasePresenter<UserInfo>
    {

        private UserInfoManager _userIfoManager;
        public new string Add(JsonReq req)
        {

            UserInfo userinfo = JsonHelper.JsonDeserialize<UserInfo>(req.Options);
            userinfo.CreateTime = DateTime.Now;
            userinfo.Status = 1;
            userinfo.Question = "";
            userinfo.Answer = "";
            userinfo.LastLoginTime = System.DateTime.Now;
            bool bl;
            string strmsg;
            try
            {
                //1.用户注册

                userinfo.Password = SdkSecurity.DesEncrypt(userinfo.Password, "newfores");
                int userid = CreateUser(userinfo);
                bl = userid != 2;
                //2.登记员工。
                EmployeeManager employeeManager = new EmployeeManager();
                EmployeeInfo employee = new EmployeeInfo();
                employee.EmployeeName = req.Options["EmployeeName"];
                int deptId = 0;
                int.TryParse(req.Options["DeptId"], out deptId);
                employee.DeptId = deptId;
                employee.DeptName = req.Options["DeptName"];
                employee.UserId = userid;
                bl = employeeManager.InsertEmployeeInfo(employee);
                strmsg = "新用户注册成功!";
            }
            catch (Exception ex)
            {
                bl = false;
                strmsg = ex.Message;
            }
            return "{ \"flag\":\"" + bl.ToString(CultureInfo.InvariantCulture) + "\",\"msg\":\"" + strmsg + "\"}";
        }
        public DataSet Get(string search)
        {
            _userIfoManager = new UserInfoManager();
            return _userIfoManager.Get(search);
        }

        public  ViewUserRoleGroupInfo GetUser(string filter)
        {
            _userIfoManager = new UserInfoManager();
            return _userIfoManager.GetUserModel(filter);
        }

        public string GetUsers(string columns, string search, int pageIndex, int pageSize, ref int pageCount, ref int totalCnt)
        {
            int total = 0;
            DataTable dt = new ManagerParent<ViewUserRoleGroupInfo>().GetSearchPaged(search, pageIndex, pageSize,
                                                                                             ref total);
            return JsonHelper.OnDataGrid(dt, totalCnt);
        }

        /// <summary>
        /// 用户是否存在
        /// </summary>
        public bool UserExists(string userName)
        {
            _userIfoManager = new UserInfoManager();
            return _userIfoManager.UserExists(userName);
        }

        /// <summary>
        /// 创建一个新用户
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int CreateUser(UserInfo model)
        {
            _userIfoManager = new UserInfoManager();
            if (!UserExists(model.UserName))
            {
                return _userIfoManager.CreateUser(model);
            }
            return 2;
        }

        /// <summary>
        /// 更新用户基本信息
        /// </summary>
        /// <name>model</name>
        /// <returns></returns>
        public int UpdateUser(UserInfo model)
        {
            _userIfoManager = new UserInfoManager();
            return _userIfoManager.UpdateUser(model);
        }

        /// <summary>
        /// 用户登录检测
        /// </summary>
        /// <param name="req">post数据对象</param>
        /// <returns></returns>
        public bool CheckLogin(JsonReq req)
        {
            _userIfoManager = new UserInfoManager();
            bool bl = _userIfoManager.CheckLogin(req.Options["UserName"], req.Options["Password"]);

            Logger.LogInfo("用户登录检测，结果为"+bl);
            return bl;
        }

        /// <summary>
        /// 更新用户登录时间
        /// </summary>
        /// <param name="id"></param>
        public void UpdateLoginTime(int id)
        {
            _userIfoManager = new UserInfoManager();
            _userIfoManager.UpdateLoginTime(id);
        }

        /// <summary>
        /// 判断用户原密码是否正确
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <param name="pwd">原密码</param>
        /// <returns></returns>
        public bool VerifyPassword(int id, string pwd)
        {
            _userIfoManager = new UserInfoManager();
            return _userIfoManager.VerifyPassword(id, pwd);
        }

        /// <summary>
        /// 更改用户密码
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <param name="pwd">新密码</param>
        /// <returns></returns>
        public bool ChangePassword(int id, string pwd)
        {
            _userIfoManager = new UserInfoManager();
            return _userIfoManager.ChangePassword(id, pwd);
        }

        /// <summary>
        /// 更新安全信息
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <param name="question">问题</param>
        /// <param name="answer">答案</param>
        /// <returns></returns>
        public bool ChangeSecureInfo(int id, string question, string answer)
        {
            _userIfoManager = new UserInfoManager();
            return _userIfoManager.ChangeSecureInfo(id, question, answer);
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="userID">用户ID</param>
        public bool DeleteUser(int userID)
        {
            _userIfoManager = new UserInfoManager();
            return _userIfoManager.DeleteUser(userID);
        }
        /// <summary>
        /// 获得用户权限信息数据列表
        /// </summary>
        /// <param name="req"></param>
        /// <returns>返回json格式数据</returns>
        public string GetUserRoleGroupList(JsonReq req)
        {
            int pageIndex = Convert.ToInt32(req["page"]);
            int pageSize = Convert.ToInt32(req["rows"]);
            int total = 0;
            //T info = JsonHelper.JsonDeserialize<T>(req.Options);
            string sql = JsonHelper.GetQueryFilterString(req.Options);
            Type type = typeof(ViewUserRoleGroupInfo);
            if (type.GetProperties().Count(a => a.Name.ToLower() == "UserID") > 0)
                sql += " order by UserID desc";
            DataTable dt = new ManagerParent<ViewUserRoleGroupInfo>().GetSearchPaged(sql, pageIndex, pageSize,
                                                                                             ref total);
            return JsonHelper.OnDataGrid(dt, total);
        }

        /// <summary>
        /// 读取角色名称
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public object GetRoleName(int roleid)
        {
            _userIfoManager = new UserInfoManager();
            return _userIfoManager.GetRoleName(roleid);
        }

        #region 角色操作

        /// <summary>
        /// 添加一用户角色
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <param name="roleID">角色ID</param>
        /// <returns></returns>
        public bool AddUserRole(int userID, int roleID)
        {
            _userIfoManager = new UserInfoManager();
            return _userIfoManager.addUserRole(userID, roleID);
        }

        /// <summary>
        /// 批量添加用户角色
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public bool AddUserRole(ArrayList list)
        {
            _userIfoManager = new UserInfoManager();
            return _userIfoManager.addUserRole(list);
        }

        /// <summary>
        /// 删除一用户角色
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <param name="roleID">角色ID</param>
        /// <returns></returns>
        public bool DeleteUserRole(int userID, int roleID)
        {
            _userIfoManager = new UserInfoManager();
            return _userIfoManager.DeleteUserRole(userID, roleID);
        }

        /// <summary>
        /// 批量删除用户角色
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public bool DeleteUserRole(ArrayList list)
        {
            _userIfoManager = new UserInfoManager();
            return _userIfoManager.DeleteUserRole(list);
        }

        /// <summary>
        /// 读取用户角色列表
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public DataSet GetUserRole(int userID)
        {
            _userIfoManager = new UserInfoManager();
            return _userIfoManager.GetUserRole(userID);
        }

        /// <summary>
        /// 读取用户角色列表
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public ArrayList GetUserRoleArray(int userID)
        {
            _userIfoManager = new UserInfoManager();
            return _userIfoManager.GetUserRoleArray(userID);
        }
        /// <summary>
        /// 根据条件得到用户对象实体
        /// </summary>
        /// <param name="filter">过滤条件</param>
        /// <returns>返回json格式的数据</returns>
        public string GetUserModel(string filter)
        {
            _userIfoManager = new UserInfoManager();
            ViewUserRoleGroupInfo view = _userIfoManager.GetUserModel(filter);
            return JsonHelper.JsonSerializer(view);
        }
        #endregion
    }
}
